*-------------------------------------------------------------------------------*
* DIRECTORIES
*-------------------------------------------------------------------------------*
local l_direc_data `1'
local l_direc_code `2' 
local l_direc_figtab `3' 

local direc_data_raw  `l_direc_data'/raw
local direc_data_clean `l_direc_data'/clean



* * * * * * * * * * * * *
* * * * * * * * * * * * *
*COSINE DATA

use `direc_data_raw'/editorJournalCosineDistances_1.dta, clear
forval i=2/10 {
	append using `direc_data_raw'/editorJournalCosineDistances_`i'.dta
}
keep if journal_year == editor_year & journal_year <= 2009
keep journal_year journal_nlmid editor_id author_startdate author_enddate cosine

rename editor_id e__id
rename author_start e_firstpubyear
rename author_end e_lastpubyear

rename journal_nlmid j__nlmid
egen j_firstpubyear = min(journal_year) , by(j__nlmid)
egen j_lastpubyear = max(journal_year) , by(j__nlmid)

rename journal_year y__year

rename cosine ejy_cosine

compress
tempfile T_cosine
save `T_cosine'


* * * * * * * * * * * * *
* * * * * * * * * * * * *
*XWALK
import delimited "`direc_data_raw'/author_editor_crosswalk.csv", clear

replace journal_nlmid = subinstr(journal_nlmid,"R","",.)
destring journal_nlmid, replace
rename journal_nlmid j__nlmid

rename editor_id e__id

rename editor_star ej_firstedyear
rename editor_end ej_lastedyear

keep j__nlmid e__id ej_firstedyear ej_lastedyear

tempfile T_xwalk
save `T_xwalk'


* * * * * * * * * * * * *
* * * * * * * * * * * * *
*BUILD FILLEDIN DATA
use `T_cosine', clear
keep j_* 
gduplicates drop
tempfile T_j
save `T_j'

use `T_cosine', clear
keep e_* 
gduplicates drop
tempfile T_e
save `T_e'

use `T_cosine', clear
keep e__ j__ y__ ejy_*
gduplicates drop
tempfile T_ejy
save `T_ejy'


use `T_cosine', clear
keep j__nlmid e__id
gduplicates drop
gen y__year = _n + 1960
replace y__year = . if y__year < 1960 | y__year > 2009
fillin j__nlmid e__id y__year
drop _f
drop if y__year == .

joinby e__id using `T_e'
joinby j__nlmid using `T_j'

joinby e__id j__nlmid y__year using `T_ejy', unmatched(both)
tab _m
drop _m

joinby j__nlmid e__id using `T_xwalk', unmatched(both)
tab _m
drop _m

keep if y__year>=j_firstpubyear & ///
		y__year<=j_lastpubyear & ///
		y__year>=e_firstpubyear & ///
		y__year<=e_lastpubyear	

* * * * * * * * * * * * *
* * * * * * * * * * * * *
*RECODE
recode ejy_cosine (.=0)

*INDICATORS
gen ej_evereditor = ej_firstedyear!=.
gen ejy_curreditor = y__year>=ej_firstedyear & y__year<=ej_lastedyear
gen ejy_preeditor = y__year<ej_firstedyear
gen ejy_psteditor = y__year>ej_lastedyear
gen ejy_ysincest = y__year - ej_firstedyear

*GROUPS
egen I_e = group(e__)
egen I_j = group(j__)
egen I_y = group(y__)
egen I_ej = group(I_e I_j)
egen I_ey = group(I_e I_y)
egen I_jy = group(I_j I_y)



*PLOT1
local l_BW 0.015
tw 	(kdensity ejy_cosine if !ej_evereditor, bw(`l_BW') lw(medthick) )	///
	(kdensity ejy_cosine if ej_evereditor & ejy_preeditor, bw(`l_BW') lw(thick) lpat(shortdash) )	///
	(kdensity ejy_cosine if ej_evereditor & ejy_curreditor, bw(`l_BW') lw(thick) lpat(solid))	///
	(kdensity ejy_cosine if ej_evereditor & ejy_psteditor, bw(`l_BW') lw(thick) lpat(longdash))	///
	, ///
	legend(order(	1 "Never editor, all years" ///
					2 "Ever editor, pre" ///
					3 "Ever editor, active" ///
					4 "Ever editor, post") col(2) position(6) size(small)) ///
	ylab(, angle(horiz) format(%3.1fc) grid gmax gmin) ///
	ytitle("Density")  ///
	xtitle("Editor-journal similarity", margin(small)) ///
	xlab(, format(%3.1fc) grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	scale(1.25) xsize(8) ysize(5) ///
	name(appendixg1, replace)
gr export "`l_direc_figtab'/fig_altapproach_distrib.pdf", as(pdf) replace		


*REG
cls
preserve
	
	gen X = ejy_curreditor
	gen Y = ejy_cosine
	
	reghdfe Y X , a(I_ey I_ej I_jy) 
	keep if e(sample)
			
	* I_y	
	qui {
		reghdfe Y X , a(I_y) 
		local l_r2tot = e(r2)
		reghdfe Y X , noabsorb
			local estflg_par2_y = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a(I_y)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(I_y) cluster(I_ej)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_y {
			estadd scalar `l_flg' = ``l_flg''
		}
	
	* I_y I_ej
	qui {
		reghdfe Y X , a(I_y I_ej) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(I_y) 
			local estflg_par2_ej = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(I_ej) 
			local estflg_par2_y = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( I_y I_ej)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(I_y I_ej) cluster(I_ej)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_y estflg_par2_ej {
			estadd scalar `l_flg' = ``l_flg''
		}

	* I_ey I_ej I_jy
	qui {
		reghdfe Y X , a(I_ey I_ej I_jy) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(I_ey I_jy) 
			local estflg_par2_ej = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(I_ej) 
			local estflg_par2_ey_jy = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( I_ey I_ej I_jy)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(I_ey I_ej I_jy) cluster(I_ej)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_ey_jy estflg_par2_ej {
			estadd scalar `l_flg' = ``l_flg''
		}
		
	esttab using"`l_direc_figtab'/tab_altapproach.tex", keep(X) label mlabels(none) ///
		b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
		scalars(estflg_r2tot estflg_par2_y estflg_par2_ej estflg_par2_ey_jy estflg_par2_X) ///
		sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
		style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		replace
	eststo clear	
restore



*PLOT2
preserve	
	gen tenure = ej_lastedyear - ej_firstedyear
	su tenure
	local l_avgtenure = `r(mean)'
	
	local l_lagmin = -10
	local l_lagmax = 10
	
	keep if ejy_ysincest>=`l_lagmin' & ejy_ysincest<=`l_lagmax'
	replace ejy_ysincest = ejy_ysincest + abs(`l_lagmin')
	
	su ejy_ysincest
	forval i=`r(min)'/`r(max)' {
		if `i'!=abs(`l_lagmin') {
			gen Ievent_`i' = ejy_ysincest==`i'
		}
	}
	
	reg ejy_cosine Ievent_* , vce(cluster I_ej)
	regsave , ci
	
	drop if var == "_cons"
	replace var = subinstr(var,"Ievent_","",.)
	destring var , replace
	replace var = var - abs(`l_lagmin')
	su var
	local l_varstart = `r(min)'
	
	count
	local l_NewN = `r(N)'+1
	set obs `l_NewN'
	recode coef var (.=0)
	
	tw	(scatter coef var, mc(maroon)) ///
		(rcap ci_* var, color(maroon)) ///
		,	///
		yline(0, lc(black)) ///
		xline(`l_avgtenure', lc(gs10) lpat(dash)) ///
		legend(off) ///
		ylab(, angle(horiz) format(%4.2fc) grid gmax gmin) ///
		ytitle("Estimate/95% C.I.") ///
		xtitle("Years relative to editorial start", margin(small)) ///
		xlab(, grid gmax gmin)  ///		
		graphregion(color(white) lc(white)) bgcolor(white) ///
		scale(1.25) xsize(8) ysize(5) ///
		name(appendixg2, replace)
	gr export "`l_direc_figtab'/fig_altapproach_eventstud.pdf", as(pdf) replace	

restore	
